package appfuse.framework.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>Title:时间处理组件</p>
 * <p>Description:对时间的转换、间隔进行处理的方法</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author savage
 * @since 2005-11-24
 */

/**
     * 以指定的格式来格式化日期
     *
     * @param date Date
     * @param format String
     * @return String
     */
public class TimeUtil {
	
	/**取得系统日期*/
	public static String getDate()
	{
		SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");			
		String date = time.format(new Date());
		return date;
	}
	
	/**取得系统时间*/
	public static String getDateTime()
	{
		SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");			
		String dateTime = time.format(new java.util.Date());
		return dateTime;
	}
	/**
	 * 得到上个星期天的日期
	 * @return
	 */
	public static String getLastSunday(){
		Calendar cal = Calendar.getInstance();
		int weeknum = cal.get(Calendar.DAY_OF_WEEK);
		
		if(weeknum == 1)
			weeknum = 8;
		SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
		return  sdf3.format(TimeUtil.addDate(new Date(), -weeknum+1));
	}
	
	/** 
	 * 得到上个星期一的日期
	 * @return
	 */
	public static String getLastMonday(){
		Calendar cal = Calendar.getInstance();
		int weeknum = cal.get(Calendar.DAY_OF_WEEK);
		if(weeknum == 1)
			weeknum = 8;
		SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
		return  sdf3.format(TimeUtil.addDate(TimeUtil.addDate(new Date(), -weeknum+1), -6));
	}
	/**
	 * 得到所有星期一和星期天集合
	 * @return
	 */
	public static List<Date>  getMondayAndSundayInMonth(int year,int month){
		//年-月字符串
		String yearMonth = new Integer(year).toString()+"-"+new Integer(month).toString()+"-";
		//第一个星期一是几号
		int firstMonday = getDay(year, month, 1);
		List<Date>  days = new ArrayList<Date>();
		int result = firstMonday;
		while(result <= TimeUtil.getMonthDayNum()){
			//加入星期一
			Date date1 = TimeUtil.timeToDate(TimeUtil.stringToTime(yearMonth+new Integer(result).toString()));
			days.add(date1);
			//加入星期天
			days.add(TimeUtil.addDate(date1, 6));
			result = result + 7;
		}
		return days;
	}
	
	/**
	 * 得到所有星期天集合
	 * @return
	 */
	public static List<Date>  getSundayInMonth(int year,int month){
		//年-月字符串
		String yearMonth = new Integer(year).toString()+"-"+new Integer(month).toString()+"-";
		//第一个星期一是几号
		int firstMonday = getDay(year, month, 1);
		List<Date>  days = new ArrayList<Date>();
		int result = firstMonday;
		while(result <= TimeUtil.getMonthDayNum()){
			//加入星期一
			Date date1 = TimeUtil.timeToDate(TimeUtil.stringToTime(yearMonth+new Integer(result).toString()));
			//加入星期天
			days.add(TimeUtil.addDate(date1, 6));
			result = result + 7;
		}
		return days;
	}
	
	/**
	 * 得到本月天数
	 * @return
	 */
	public static int getMonthDayNum(){
		Calendar cal = Calendar.getInstance();
		int dayofmonth = cal.get(cal.DATE);
		cal.add(cal.MONTH, 1);
		dayofmonth = cal.get(cal.DATE);
		cal.add(cal.DATE, -dayofmonth);
		return cal.getTime().getDate();
	}
	
	//得到月天数
	public static int getMonthDayNumByMonth(int month){
		Calendar cal = Calendar.getInstance();
		int dayofmonth = cal.get(cal.DATE);
		cal.set(cal.MONTH, month);
		dayofmonth = cal.get(cal.DATE);
		cal.add(cal.DATE, -dayofmonth);
		return cal.getTime().getDate();
		
	}
	
	
	/**
	 * 得到第X周的第X日是几号
	 * @return
	 */
	public static Date getDateByWeekAndDay(int week,int day,int month,int year){
		
		int firstMonday = getDay(year,month,1);
		
		int result = 0;
		if(week <= 1){
			result = firstMonday + day - 1;
		}else{
			result = firstMonday + ((week-1)*7) + day - 1;
		}
		
		String date = new Integer(year).toString()+"-"+new Integer(month).toString()+"-"+new Integer(result).toString();
		
		return TimeUtil.stringToDate(date+" 00:00:00.000000000");
	}
	//得到第一个星期X是几号
	private static int getDay(int year, int month, int xingqi) {
		int j = xingqi + 1;
		// //由于星期天排在第一，所以需要特殊处理
		if (xingqi == 7) {
			j = 1;
		}
		Calendar cal = Calendar.getInstance();
		cal.set(cal.YEAR, year);
		cal.set(cal.MONTH, month - 1);
		// 14天内,必然出现星期一到星期天
		for (int i = 1; i < 15; i++) {
			cal.set(cal.DAY_OF_MONTH, i);
			if (cal.get(cal.DAY_OF_WEEK) == j) {
				return cal.get(cal.DAY_OF_MONTH);
			}
		}
		return 99;
	}
	
	
//	获取yyyy-MM-dd是星期几,返回值为1，2，3，4，5，6，7
	public static int getWeekdayOfDateTime(String datetime){
	   DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
	   Calendar c = Calendar.getInstance();   
	      try {
	  c.setTime(df.parse(datetime));
	   } catch (Exception e) {
	  e.printStackTrace();
	   }
	   int weekday = c.get(Calendar.DAY_OF_WEEK)-1;
	   if(weekday == 0)
		   weekday = 7;
	   return weekday;
	} 
	
	
	/**
	 * 把字符串yy-MM-dd 转成Timestamp
	 * 
	 * @param date
	 *            日期 yy-MM-dd
	 * @return 返回时间戳
	 */
	public static Timestamp stringToTime(String date) {
		Timestamp timestamp = null;
		String dateStringToParse = date + " 00:00:00.000000000";
		try{
			timestamp =  Timestamp.valueOf(dateStringToParse); 
		}catch (Exception e) {
			timestamp = null;
		}
		
		return timestamp;
	}
	
	/**
	 * 把字符串yy-MM-dd aa:bb:cc.d 转成Date
	 */
	public static Date stringToDate(String date) {
		Timestamp timestamp  =  Timestamp.valueOf(date); 
		Date dateReult = new Date(timestamp.getTime());		
		return dateReult;
	}
	
	
	/**
	 * 把Timestamp转成date
	 * 
	 * @param time
	 *            时间戳
	 * @return 返回日期
	 */
	public static Date timeToDate(Timestamp time) {
		Date data = new Date(time.getTime());
		return data;
	}
	
	/**
	 * 返回年份
	 * 
	 * @param date
	 *            日期
	 * @return 返回年份
	 */
	public static int getYear(Date date) {
	   Calendar c = Calendar.getInstance();
	   c.setTime(date);
	   return c.get(Calendar.YEAR);
	}

	/**
	 * 返回月份
	 * 
	 * @param date
	 *            日期
	 * @return 返回月份
	 */
	public static int getMonth(Date date) {
	   Calendar c = Calendar.getInstance();
	   c.setTime(date);
	   return c.get(Calendar.MONTH) + 1;
	}

	/**
	 * 返回日份
	 * 
	 * @param date
	 *            日期
	 * @return 返回日份
	 */
	public static int getDay(Date date) {
	   Calendar c = Calendar.getInstance();
	   c.setTime(date);
	   return c.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 返回小时
	 * 
	 * @param date
	 *            日期
	 * @return 返回小时
	 */
	public static int getHour(Date date) {
	   Calendar c = Calendar.getInstance();
	   c.setTime(date);
	   return c.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 返回分钟
	 * 
	 * @param date
	 *            日期
	 * @return 返回分钟
	 */
	public static int getMinute(Date date) {
	   Calendar c = Calendar.getInstance();
	   c.setTime(date);
	   return c.get(Calendar.MINUTE);
	}

	/**
	 * 返回秒钟
	 * 
	 * @param date
	 *            日期
	 * @return 返回秒钟
	 */
	public static int getSecond(Date date) {
	   Calendar c = Calendar.getInstance();
	   c.setTime(date);
	   return c.get(Calendar.SECOND);
	}

	/**
	 * 返回毫秒
	 * 
	 * @param date
	 *            日期
	 * @return 返回毫秒
	 */
	public static long getMillis(Date date) {
	   Calendar c = Calendar.getInstance();
	   c.setTime(date);
	   return c.getTimeInMillis();
	}
	/**
	 * 日期相加
	 * 
	 * @param date
	 *            日期
	 * @param day
	 *            天数
	 * @return 返回相加后的日期
	 */
	public static Date addDate(Date date, int day) {
	   Calendar c = Calendar.getInstance();
	   c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
	   return c.getTime();
	}
	
	/**
	 * 日期相减
	 * 
	 * @param date
	 *            日期
	 * @param day
	 *            天数
	 * @return 返回相减后的日期
	 */
	public static Date minDate(Date date, int day) {
	   Calendar c = Calendar.getInstance();
	   c.setTimeInMillis(getMillis(date) - ((long) day) * 24 * 3600 * 1000);
	   return c.getTime();
	}


	/**
	 * 日期相减
	 * 
	 * @param date
	 *            日期
	 * @param date1
	 *            日期
	 * @return 返回相减后的日期
	 */
	public static int diffDate(Date date, Date date1) {
	   return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
	}

	/**
	 * 和目前的时间间隔
	 * 
	 * @param date
	 *            日期
	 * @return 返回相减后的日期
	 */
	public static String disDate(Timestamp time) {
		String result = "";
		String state = "";
		if(time == null) 
			return result;
		Date date = new Date(time.getTime());
		long DisDate = System.currentTimeMillis() - getMillis(date);
		if(System.currentTimeMillis() > getMillis(date)){
			state = "前";
		}else{
			DisDate = -DisDate;
			state = "后";
		}
		
		if(DisDate >= (24 * 3600 * 1000)){
			long day = DisDate/(24 * 3600 * 1000);
			result = Integer.toString((int) (day)) + "天" + state;
			if(result.equals("2天前"))
				result = "前天";
			if(result.equals("1天前"))
				result = "昨天";
			if(result.equals("1天后"))
				result = "明天";
			if(result.equals("2天前"))
				result = "后天";
		}
		else if(DisDate >= (3600 * 1000)) {
			long hour = DisDate/(3600 * 1000);
			result = Integer.toString((int) (hour)) + "小时" + state;
		}
		else if(DisDate >= (60 * 1000)) {
			long minite = DisDate/(60 * 1000);
			result = Integer.toString((int) (minite)) + "分钟" + state;
		} else {
			result = "一分钟内";
		}
		return result;
	}
	
	
	/**
	 * 和目前的时间间隔，比赛专用
	 * 
	 * @param date
	 *            日期
	 * @return 返回相减后的日期
	 */
	public static String disDateForMatch(Timestamp time) {
		String result = "";
		String state = "";
		if(time == null) 
			return result;
		Date date = new Date(time.getTime());
		long DisDate = System.currentTimeMillis() - getMillis(date);
		if(System.currentTimeMillis() > getMillis(date)){
			state = "前";
		}else{
			DisDate = -DisDate;
			state = "后";
		}
		
		if(DisDate >= (24 * 3600 * 1000)){
			long day = DisDate/(24 * 3600 * 1000);
			result = Integer.toString((int) (day)) + "天" + state;
			if(result.equals("2天前"))
				result = "前天";
			if(result.equals("1天前"))
				result = "昨天";
			if(result.equals("1天后"))
				result = "明天";
			if(result.equals("2天前"))
				result = "后天";
		}
		else if(DisDate >= (3600 * 1000)) {
			long hour = DisDate/(3600 * 1000);
			result = Integer.toString((int) (hour)) + "小时" + state;
			if(result.equals("2小时前"))
				result = "进行中";
		} else if(DisDate >= (60 * 1000)) {
			long minite = DisDate/(60 * 1000);
			if(state.equals("前")) {
				result = "进行中";
			}else{
				result = Integer.toString((int) (minite)) + "分钟" + state;
			}
		} else {
			result = "即将开始";
		}
		return result;
	}
	
	/**
	 * 是否在今天，世界杯时间
	 * 
	 * @param date
	 *            日期
	 * @return 返回相减后的日期
	 */
	public static int isToday(Timestamp time) {;
		int result = 0;
		if(time == null) 
			return result;
		
		Date objDate = new Date(time.getTime());
		
		SimpleDateFormat formatTime = new SimpleDateFormat("yyyy-MM-dd");
		
		String now = formatTime.format(new Date());
		String date = formatTime.format(objDate);
		
		if(date.equals(now))
			result = 1;
		
		return result;
	}
	
	/**
	 * 是否在昨天，
	 * 
	 * @param date
	 *            日期
	 * @return 返回相减后的日期
	 */
	public static int isYesterday(Timestamp time) {;
		int result = 0;
		if(time == null) 
			return result;
		
		Calendar   cal   =   Calendar.getInstance();  
		cal.add(Calendar.DATE,   -1);		
		Date objDate = new Date(time.getTime());		
		SimpleDateFormat formatTime = new SimpleDateFormat("yyyy-MM-dd");		
		String lastDay = formatTime.format(cal.getTime());

		String date = formatTime.format(objDate);
		
		if(date.equals(lastDay))
			result = 1;
		
		return result;
	}
}
